
package JET.graphics;

import JET.physics.bodies.SpringBody;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import JET.Centers.ControlCenter;
import JET.Frames.DataModels.DataModelsFactory;
import JET.SA.SA_RigidBody;
import JET.SA.SA_Selection;
import JET.SA.SA_SpringBody;
import JET.Scene;
import JET.VirtualKeyboard.VKey;
import JET.physics.bodies.RigidBody;
import java.awt.Component;
import javax.script.ScriptException;

/**
 *
 * @author 057Ry
 */
public class GraphicPanelKeyListener implements KeyListener
{
    static final ControlCenter CC = ControlCenter.getInstance();
    private Component owner;

    // Name of a scene that this key listeenrs is added to
    private String sceneName;
    private Scene scene;

    public GraphicPanelKeyListener(Component owner) {
        this.owner = owner;
        this.sceneName = ((GraphicPanel)owner).getSceneName();
        this.scene = CC.getScene(sceneName);
    }

    public void keyTyped(KeyEvent e) {
        // Notify JavaScript of this key typed event
        VKey vKey = scene.getVKeyboard().getKey(e.getKeyCode());
        try {
            scene.SC.getJsEngine().eval("JetSystem.callOnKeyTypedListeners('" + vKey + "', " + e.getKeyCode() + ")");
        } catch (ScriptException ex) {
            CC.GUIC.printlnToOutput("[Ex] Exception while notifying onKeyTyped listeners. [key: "+vKey+"]");
            ex.printStackTrace();
        }
    }

    public void keyPressed(KeyEvent e)
    {
        if (!CC.MC.getMouseModeSequence().isEmpty() &&
            e.getKeyCode() == CC.MC.getMouseModeSequence().getCurentTask().getKeyCode())
        {
            CC.MC.getMouseModeSequence().proceed();
        }
        else
        {
            if (!e.isAltDown() && !e.isControlDown()) {
                scene.getVKeyboard().keyPressed(e.getKeyCode(), scene.getJETContext().getTime());
                
                switch( e.getKeyCode() ) {
                    case KeyEvent.VK_SPACE :
                        if ( CC.animationInProgress )
                            CC.stop(sceneName);
                        else
                            CC.play(sceneName);
                        break;
                    case KeyEvent.VK_DELETE :
                        if ( !scene.EC.selectionGroup.isEmpty() ) {
                            SA_Selection.deleteSelectedObjects(scene, true);
                        } else if (scene.EC.currentBody instanceof SpringBody) {
                            SA_SpringBody.removeSpringBody((SpringBody) scene.EC.currentBody, scene);
                            DataModelsFactory.getSceneTreeModel().fireSpringBodyListChanged();
                        } else if (scene.EC.currentBody instanceof RigidBody) {
                            SA_RigidBody.removeRigidBody((RigidBody) scene.EC.currentBody, scene);
                            //DataModelsFactory.getSceneTreeModel().fireSpringBodyListChanged();
                        }
                        
                        //scene.GC.updateView();
                        owner.repaint();
                        break;
                    case KeyEvent.VK_ESCAPE : SA_Selection.clearSelection(scene); break;
                }
            }
            else if (e.isControlDown())
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_EQUALS :
                        GraphicPanelHelper.scaleGraphicPanel((GraphicPanel)owner, -1, CC.MC.mouseEvent, false, CC);
                        //scene.GC.updateView();
                        owner.repaint();
                        break;
                    case KeyEvent.VK_MINUS :
                        GraphicPanelHelper.scaleGraphicPanel((GraphicPanel)owner, 1, CC.MC.mouseEvent, false, CC);
                        //scene.GC.updateView();
                        owner.repaint();
                        break;
                }
        }
    }

    public void keyReleased(KeyEvent e)
    {
        if (!e.isAltDown() && !e.isControlDown())
            scene.getVKeyboard().keyReleased(e.getKeyCode(), scene.getJETContext().getTime());
    }
}